using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class ProductDetailControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void ProductDetailController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexProductDetailViewModel);
        }

        [TestMethod]
        public virtual void ProductDetailController_LoadTest()
        {
            ProductDetailServiceUtil.CreateProductDetail();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void ProductDetailController_DeleteTest()
        {
            ProductDetail productDetail = ProductDetailServiceUtil.CreateProductDetail();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.Delete(productDetail.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void ProductDetailController_ReportTest()
        {
            ProductDetail productDetail = ProductDetailServiceUtil.CreateProductDetail();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void ProductDetailController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateProductDetailViewModel);
        }

        [TestMethod]
        public virtual void ProductDetailController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            ProductDetail productDetail = ProductDetailServiceUtil.GetProductDetail();
            CreateProductDetailViewModel viewModel = Mapper.Map<ProductDetail, CreateProductDetailViewModel>(productDetail);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void ProductDetailController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            ProductDetail productDetail = ProductDetailServiceUtil.CreateProductDetail();

            var result = controller.Edit(productDetail.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditProductDetailViewModel);
            Assert.AreEqual((result.Model as EditProductDetailViewModel).Stock, productDetail.Stock);
        }

        [TestMethod]
        public virtual void ProductDetailController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            ProductDetail productDetail = ProductDetailServiceUtil.CreateProductDetail();
            productDetail.Stock = 1;

            EditProductDetailViewModel viewModel = Mapper.Map<ProductDetail, EditProductDetailViewModel>(productDetail);
            controller.Edit(viewModel);

            var result = controller.Edit(productDetail.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditProductDetailViewModel).Stock, 1);
        }

        [TestMethod]
        public virtual void ProductDetailController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            ProductDetail productDetail = ProductDetailServiceUtil.CreateProductDetail();

            var result = controller.Details(productDetail.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsProductDetailViewModel);
            Assert.AreEqual((result.Model as DetailsProductDetailViewModel).Stock, productDetail.Stock);
        }

		/* Details region test */
        [TestMethod]
        public virtual void ProductDetailController_LoadOrderDetailsTest()
        {
            OrderDetail orderDetails = OrderDetailServiceUtil.CreateOrderDetailFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            var result = controller.LoadOrderDetails("", "", 0, 10, "", 1, orderDetails.ProductDetail.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void ProductDetailController_AddOrderDetailsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            OrderDetail orderDetails = OrderDetailServiceUtil.GetOrderDetailFull();
            PopUpOrderDetailViewModel viewModel = Mapper.Map<OrderDetail, PopUpOrderDetailViewModel>(orderDetails);

            var result = controller.AddOrderDetails(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void ProductDetailController_GetEditOrderDetailsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            OrderDetail orderDetails = OrderDetailServiceUtil.CreateOrderDetailFull();

            var result = controller.EditOrderDetails(orderDetails.Id, orderDetails.ProductDetail.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/OrderDetail/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpOrderDetailViewModel);
			Assert.AreEqual((result.Model as PopUpOrderDetailViewModel).Quantity, orderDetails.Quantity);
            
        }

        [TestMethod]
        public virtual void ProductDetailController_PostEditOrderDetailsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());
            OrderDetail orderDetails = OrderDetailServiceUtil.CreateOrderDetailFull();
            orderDetails.Quantity = 1;

            PopUpOrderDetailViewModel viewModel = Mapper.Map<OrderDetail, PopUpOrderDetailViewModel>(orderDetails);
            controller.EditOrderDetails(viewModel);

            var result = controller.EditOrderDetails(orderDetails.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpOrderDetailViewModel).Quantity, 1);
        }

        [TestMethod]
        public virtual void ProductDetailController_DeleteOrderDetailsTest()
        {
            OrderDetail orderDetails = OrderDetailServiceUtil.CreateOrderDetailFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<ProductDetailController>());

            controller.DeleteOrderDetails(orderDetails.Id, orderDetails.ProductDetail.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

